// subpage/pages/balance/refund.js
const app = getApp();
import service from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    applyPrice: 0,
    nowPrice: 0,
    refundTotal: 0,
    freeze: 0,
    balance: 0,
    isRefund: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getBalance()
  },
  bindKeyInput: function (e) {
    this.setData({
      applyPrice: e.detail.value
    })
  },
  editPriceAll(){
    this.setData({
      applyPrice: this.data.nowPrice
    })
  },
    //我的余额
    getBalance() {
      let that = this
      let res =  service("GET", '/wallet/money', {}).then(res => {
        // console.log('初始化金额', res)
        let price = Math.ceil(30 - res.data.money.balance)
        console.log('初始化金额', res)
        let now = (res.data.money.balance  - res.data.money.freeze).toFixed(2)
        now = now > 0 ? now : 0
        if (res.data.code == 0) {
          that.setData({
            balance: res.data.money.balance,
            freeze: res.data.money.freeze,
            low: 30,
            upPrice: price,
            nowPrice: now
          })
          // that.getApply()
        }
      })
    },

  //发起退款
  refund(){
    let that = this
    let price = parseFloat(this.data.applyPrice)
    let now =parseFloat(this.data.nowPrice)
    if(price > now){
      console.log('refund',typeof price,typeof now)
      wx.showToast({
        title: '余额不足',
        icon:'none'
      })
      return false
    }
    if(this.data.applyPrice == ''){
      wx.showToast({
        title: '请输入退款金额',
        icon:'none'
      })
      return false
    }
    if(this.data.applyPrice <= 0){
      wx.showToast({
        title: '退款金额不正确',
        icon:'none'
      })
      return false
    }
    if(this.data.isRefund) return false
    this.setData({
      isRefund: true
    })
    let res =  service("POST", '/wallet/refunded/apply', this.data.applyPrice).then(res => {
      console.log('退款', res)
      if(res.data.code ==0){
        wx.showToast({
          title: res.data.msg,
          icon:'success',
        })
        that.setData({
          isRefund: false,
          applyPrice: 0
        })
        setTimeout(() => {
          that.getBalance()
        }, 500);
        
      }else{
        wx.showToast({
          title: res.data.msg,
          icon:'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})